package com.wolfpire.knowledge.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.knowledge.dao.KnowledgeDao;
import com.wolfpire.knowledge.dao.SubjectKnowledgeRelationDao;
import com.wolfpire.knowledge.service.KnowledgeService;
import com.wolfpire.profession.dao.ProfessionDao;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.MD5;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.dao.AuthorityDao;
import com.wolfpire.system.dao.OrgDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.dao.RoleDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.dao.UserRoleRelationDao;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.Knowledge;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.SubjectKnowledgeRelation;
import com.wolfpire.system.model.User;
import com.wolfpire.system.model.UserRoleRelation;
import com.wolfpire.system.service.UserService;

@Service("knowledgeService")
public class KnowledgeServiceImpl extends BaseHibernateService<Knowledge, Long> implements
	KnowledgeService {

	@Resource private KnowledgeDao knowledgeDao;
	@Resource private SubjectDao subjectDao;
	@Resource private SubjectKnowledgeRelationDao subjectKnowledgeRelationDao;
	
	public List<Knowledge> list(Knowledge entity) {
		// TODO Auto-generated method stub
		return this.knowledgeDao.list(entity);
	}

	public Knowledge getByName(String name) {
		// TODO Auto-generated method stub
		return this.knowledgeDao.getByName(name);
	}

	public Page<Knowledge> setPageDataList(Page<Knowledge> page,
			Knowledge filterKnowledge) {
		// TODO Auto-generated method stub
		return this.knowledgeDao.findKnowledges(page, filterKnowledge);
	}

	public List<Knowledge> getKnowledgesBySubjectId(Long subjectId) {
		// TODO Auto-generated method stub
		return null;
	}

	public Knowledge getCompleteKnowledge(Long id) {
		Knowledge knowledge = this.get(id);
		// 绑定专业
		SubjectKnowledgeRelation sk = this.subjectKnowledgeRelationDao.getSKRelation(id);
		knowledge.setSubjectId(sk.getSubjectId());
		Subject subject = this.subjectDao.get(sk.getSubjectId());
		/*if (null != subject) {
			knowledge.setName(subject.getName());
		}*/
		return knowledge;
	}
	
	public void saveOrUpdate(Knowledge knowledge) {
		
		Long parentId = knowledge.getParentId();
		Long subjectId = knowledge.getSubjectId();
		Subject subject = this.subjectDao.get(subjectId);
		if (null != parentId) {
			List<Knowledge> children = this.findChildRen(parentId,subject.getName());
			//判断是否存在父组织，如果存在父组织，将其父组织设置为非叶子节点
			Knowledge parentKnowledge = this.knowledgeDao.get(parentId);
			if (CollectionUtils.isEmpty(children)) {
				parentKnowledge.setLeaf(true);
				this.knowledgeDao.saveOrUpdate(parentKnowledge);
			} else {
				if (parentKnowledge.isLeaf()) {
					parentKnowledge.setLeaf(false);
					this.knowledgeDao.saveOrUpdate(parentKnowledge);
				}
			}
		}
		
		if (null == knowledge.getId()) {
			List<Knowledge> children = this.findChildRen(knowledge.getId(),subject.getName());
			if (!CollectionUtils.isEmpty(children)) {
				//判断是否存在子节点，如果存在，将其设置为非叶子节点
				knowledge.setLeaf(false);
			}
			this.knowledgeDao.saveOrUpdate(knowledge);
		}else{
			this.knowledgeDao.merge(knowledge);
		}
		
		/*if (null == knowledge.getId()) {
			this.knowledgeDao.saveOrUpdate(knowledge);
		} else {
			this.knowledgeDao.merge(knowledge);
		} */
		
		Long knowledgeId = knowledge.getId();
		//Long subjectId = knowledge.getSubjectId();
		this.subjectKnowledgeRelationDao.relate(subjectId, knowledgeId);
	}
	
	public List<Knowledge> findChildRen(Long parentId,String subjectName) {
		Knowledge knowledge = new Knowledge();
		knowledge.setParentId(parentId);
		knowledge.setJoinname(subjectName);
		//knowledge.setSubjectId(subjectId);
		return this.knowledgeDao.list(knowledge);
		//return this.authorityDao.list(authority);
	}

	@Override
	protected IBaseHibernateDao<Knowledge, Long> getEntityDao() {
		// TODO Auto-generated method stub
		return this.knowledgeDao;
	}
	
}
